package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.entity.Salesman;
import com.rm.zeyou.entity.User;
import com.rm.zeyou.mapper.SalesmanConsumeDetailMapper;
import com.rm.zeyou.mapper.SalesmanMapper;
import com.rm.zeyou.mapper.ShareConfigMapper;
import com.rm.zeyou.mapper.UserMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.SalesmanService;
import com.rm.zeyou.vo.request.SelectSalesmanChannelOrderDetailVo;
import com.rm.zeyou.vo.response.SalesmanIndexResponseVo;
import com.rm.zeyou.vo.response.SelectSalesmanChannelListResponseVo;
import com.rm.zeyou.vo.response.SelectSalesmanChannelOrderDetailResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 业务员表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:08
 */
@Service
public class SalesmanServiceImpl extends ServiceImpl<SalesmanMapper, Salesman> implements SalesmanService {
    @Resource
    private SalesmanMapper salesmanMapper;
    @Resource
    private ShareConfigMapper shareConfigMapper;
    @Resource
    private SalesmanConsumeDetailMapper salesmanConsumeDetailMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public ApiRestResponse<SalesmanIndexResponseVo> salesmanIndex(User user) {
        SalesmanIndexResponseVo responseVo = new SalesmanIndexResponseVo();
        responseVo.setUserAvatar(user.getUserAvatar());
        responseVo.setRate(shareConfigMapper.getSalesmanSplitRate() + "%");
        responseVo.setUpgradeRate(shareConfigMapper.getSalesmanUpgradeSplitRate() + "%");
        responseVo.setBellRate(shareConfigMapper.getSalesmanFirstBlockSplitRate() + "%");
        responseVo.setSecondBellRate(shareConfigMapper.getSalesmanSecondBlockSplitRate() + "%");
        //收入金额
        Salesman salesman = salesmanMapper.selectOne(new LambdaQueryWrapper<Salesman>()
                .eq(Salesman::getUserId, user.getId()));
        BigDecimal addAmount = salesmanConsumeDetailMapper.selectAddAmount(salesman.getId());
        //已提现金额
        BigDecimal payoutAmount = salesmanConsumeDetailMapper.selectPayoutAmount(salesman.getId());
        responseVo.setTotalPrice(addAmount);
        responseVo.setIsPayoutPrice(payoutAmount);
        responseVo.setCanPayoutPrice(addAmount.subtract(payoutAmount));
        //总成交金额
        BigDecimal orderAmount = salesmanConsumeDetailMapper.selectOrderAmount(salesman.getId());
        responseVo.setTotalDealPrice(orderAmount);
        return ApiRestResponse.success(responseVo);
    }

    @Override
    public ApiRestResponse<PageInfo<SelectSalesmanChannelOrderDetailResponseVo>> selectSalesmanChannelOrderDetail(User user, SelectSalesmanChannelOrderDetailVo requestVo) {
       return ApiRestResponse.success( PageHelper.startPage(requestVo.getPageNum(),requestVo.getPageSize()).doSelectPageInfo(()->{
           userMapper.selectChannelList(user.getUserInvitationCode(),requestVo.getKeyword());
       }));
    }

    @Override
    public ApiRestResponse<PageInfo<SelectSalesmanChannelListResponseVo>> selectSalesmanChannelList(User user, SelectSalesmanChannelOrderDetailVo requestVo) {
        return ApiRestResponse.success( PageHelper.startPage(requestVo.getPageNum(),requestVo.getPageSize()).doSelectPageInfo(()->{
            userMapper.selectSalesmanChannelList(user.getUserInvitationCode(),requestVo.getKeyword());
        }));
    }

}
